import cPickle as pkl
import getopt
import codecs
import binarize
import cPickle as pkl

from operator import itemgetter



if __name__ == '__main__':
    import sys
    import config

    try:
        opts, args = getopt.getopt(sys.argv[1:], "c:t:")
    except getopt.GetoptError:
        print "\nUsage: train_example.py [-c config] [-t type]\n"
        sys.exit(1)

    config_file = None
    file_type = ''

    for opt in opts:
        if opt[0] == '-c':
            config_file = opt[1]
        if opt[0] == '-t':
            file_type = opt[1]

    if config_file == None or file_type == '':
        print "\nUsage: train_example.py [-c config] [-t type]\n"
        sys.exit(1)

    configSet = config.CConfig(config_file)
    read_dir = configSet.post_dir

    wordfile = open(read_dir+'word.' + file_type)
    posfile = open(read_dir+'pos.' + file_type)
    opfile = open(read_dir+'operator.' + file_type)

    ops = []
    corpus = []
    word = []
    pos = []
    nterm = []
    opres = []

    for wordline, posline, opline in zip(wordfile, posfile, opfile):
        znode = binarize.CBinarizedTreeNode()
        optmp = opline.strip().split(' ')
        wordtmp = wordline.strip().split(' ')
        postmp = posline.strip().split(' ')
        wordtmp, postmp, ntermtmp = znode.train_inst(optmp, wordtmp, postmp)
        word.extend(wordtmp)
        pos.extend(postmp)
        nterm.extend(ntermtmp)
        opres.extend(optmp)

    out_dir = configSet.feature_dir
    out_word = open(out_dir+'word_feature.' + file_type, 'w')
    out_pos = open(out_dir+'pos_feature.' + file_type, 'w')
    out_nterm = open(out_dir+'nterm_feature.' + file_type, 'w')
    out_op = open(out_dir+'op_label.' + file_type, 'w')

    for item in word:
        out_word.write(' '.join(item) + '\n')

    for item in pos:
        out_pos.write(' '.join(item) + '\n')

    for item in nterm:
        out_nterm.write(' '.join(item) + '\n')

    for item in opres:
        out_op.write(item + '\n')
